<?php

namespace app\common\model\xilumarket;

use app\common\model\User;
use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use function fast\array_get;


class ProductOffer extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'xilumarket_product_offer';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    public function product(){
        return $this->hasOne(Product::class,'id','product_id',[],'INNER')->setEagerlyType(0);
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id')->setEagerlyType(0);
    }

    public function puser(){
        return $this->hasOne(User::class,'id','puser_id',[],'LEFT')->setEagerlyType(0);
    }

    /**
     * 出价
     * @param $params
     * @return ProductOffer
     * @throws \Exception
     */
    public static function addOffer($params) {
        if(!array_get($params,'price')){
            throw new Exception("出价不得为空");
        }
        try {
            $offer = new self();
            $ret = $offer->save($params);
            if ($ret === false) {
                throw new Exception($offer->getError());
            }
        }catch (\Exception $e) {
            throw $e;
        }
        return $offer;
    }
}
